#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=100000;

int main() {
  freopen("string.in", "r", stdin);
  freopen("string.out", "w", stdout);

  static char s[maxn+1];
  int t, n;
  scanf("%d", &t);
  for (int ti=1; ti<=t; ti++) {
    scanf("%d %s", &n, s+1);

    bool flag=true;
    for (int i=1; i<=n/2 && flag; i++) {
      flag = s[i]==s[n-i+1];
    }
    if (!flag) {
      printf("1\n");
      continue;
    }

    for (int i=2; i<=n/2 && flag; i++) {
      flag = s[i]==s[i-1];
    }
    if (flag) {
      printf("-1\n");
      continue;
    }
    flag = n%2;
    for (int i=3; i<=n && flag; i++) {
      flag = s[i]==s[i-2];
    }
    if (flag) {
      printf("-1\n");
      continue;
    }

    printf("2\n");
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
